//
//  YZYSonUserListView.h
//  YZYSDKManagerDemo
//
//  Created by 赵隆杰 on 2024/6/7.
//

#import "YZYSonUserListView.h"
#import "YZYSonUserListCell.h"
#import "YZYSDKManagerHeader.h"
#import "YZYSaccountStore.h"
#import "ZCFunDataFormat.h"
#import "ZCNetworkManager.h"
#import "YZYSDKManager.h"

@interface YZYSonUserListView ()<UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, YZYSonUserListCellDelegte>

@property (nonatomic, strong) NSNumber *userId;
@property (nonatomic, copy) NSString *userName;

@property (nonatomic, strong) UIScrollView *scrollview;

@property (nonatomic, strong) UIButton *coverView;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger currentSelectedIndex;

@property (nonatomic ,strong) UIView *backbg_Add;
@property (nonatomic ,strong) UILabel *son_titleLabel;
@property (nonatomic ,strong) UITextField *sonUserTextfield;
@property (nonatomic ,strong) UIButton *defaultBtn;
@property (nonatomic ,strong) UIButton *sonUserBtn;
@property (nonatomic, strong) NSNumber *sonId_Add;//编辑小号时的小号id，区分编辑和添加

@property (nonatomic, strong) NSMutableArray *sonUserArray;

@property (nonatomic ,strong) UITextField *firstResponderTextF;//记录将要编辑的输入框
@property (nonatomic, assign) CGFloat self_Height;//保存原始的view高度
@end

@implementation YZYSonUserListView

- (instancetype)initWithUserId:(NSNumber *)userId userName:(NSString *)userName {
    if (self == [super init]) {
        self.userId = userId;
        self.userName = userName;
                
        UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, popupW, popupH)];
        scrollview.contentSize = CGSizeMake(popupW * 2, 0);
        scrollview.scrollEnabled = NO;
        [self addSubview:scrollview];
        self.scrollview = scrollview;
        
        [self initListView];
        [self initAddSonView];
        [self requestGetUserSonInfoList];
        
        //监听键盘展示和隐藏的通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)initListView {
    
    UIImageView *backbg = [[UIImageView alloc] init];
    backbg.userInteractionEnabled = YES;
    backbg.backgroundColor = [UIColor whiteColor];
    backbg.layer.masksToBounds = YES;
    backbg.layer.cornerRadius = 4;
    [self.scrollview addSubview:backbg];
    backbg.frame = CGRectMake(0, 0, popupW, popupH);
    
    UIView *nameView = [[UIView alloc] init];
    nameView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
    [backbg addSubview:nameView];
    nameView.frame = CGRectMake(0, 0, backbg.frame.size.width, 40);
    UIButton *changeBtn = [[UIButton alloc] init];
    [nameView addSubview:changeBtn];
    changeBtn.frame = CGRectMake(nameView.frame.size.width - 70 - 15, 0, 70, nameView.frame.size.height);
    [changeBtn setTitle:@"切换账号" forState:UIControlStateNormal];
    [changeBtn setTitleColor:yzy_labelColor forState:UIControlStateNormal];
    changeBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [changeBtn addTarget:self action:@selector(didClickChangeBtn) forControlEvents:UIControlEventTouchUpInside];
    UIButton *delegateBtn = [[UIButton alloc] init];
    [nameView addSubview:delegateBtn];
    delegateBtn.frame = CGRectMake(CGRectGetMinX(changeBtn.frame) - 5 - 70, 0, 70, nameView.frame.size.height);
    [delegateBtn setTitle:@"删除账号" forState:UIControlStateNormal];
    [delegateBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    delegateBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [delegateBtn addTarget:self action:@selector(didClickDelegateBtn) forControlEvents:UIControlEventTouchUpInside];
    UILabel *usernameLabel = [[UILabel alloc] init];
    usernameLabel.font = [UIFont systemFontOfSize:16];
    [nameView addSubview:usernameLabel];
    usernameLabel.frame = CGRectMake(15, 0, CGRectGetMinX(delegateBtn.frame) - 5 - 15, nameView.frame.size.height);
    usernameLabel.text = self.userName;
    
    UIView *titleView = [[UIView alloc] init];
    [backbg addSubview:titleView];
    titleView.frame = CGRectMake(0, CGRectGetMaxY(nameView.frame), backbg.frame.size.width, 40);
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = [UIFont boldSystemFontOfSize:16];
    titleLabel.frame = CGRectMake(20, 0, 120, 40);
    titleLabel.text = @"请选择账号登录";
    titleLabel.textColor = yzy_labelColor;
    [titleView addSubview:titleLabel];
    UIButton *addBtn = [[UIButton alloc] init];
    [titleView addSubview:addBtn];
    addBtn.frame = CGRectMake(titleView.frame.size.width - 90 - 15, 0, 90, titleView.frame.size.height);
    [addBtn setTitle:@"添加小号" forState:UIControlStateNormal];
    [addBtn setImage:[[YZYSaccountStore sharedInstance] getImage:@"round_add"] forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor colorWithRed:81/255.0 green:184/255.0 blue:191/255.0 alpha:1.0] forState:UIControlStateNormal];
    addBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [addBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 5)];
    [addBtn addTarget:self action:@selector(didClickAddBtn) forControlEvents:UIControlEventTouchUpInside];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(titleView.frame), backbg.frame.size.width, backbg.frame.size.height - CGRectGetMaxY(titleView.frame)) style:UITableViewStylePlain];
    tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerClass:[YZYSonUserListCell class] forCellReuseIdentifier:NSStringFromClass([YZYSonUserListCell class])];
    [backbg addSubview:tableView];
    self.tableView = tableView;
    
    //默认选中第一行
    self.currentSelectedIndex = 0;
}

- (void)initAddSonView {
    
    UIView *backbg_Add = [[UIView alloc] init];
    backbg_Add.userInteractionEnabled = YES;
    backbg_Add.backgroundColor = [UIColor whiteColor];
    backbg_Add.layer.masksToBounds = YES;
    backbg_Add.layer.cornerRadius = 4;
    [self.scrollview addSubview:backbg_Add];
    backbg_Add.frame = CGRectMake(popupW, 0, popupW, popupH);
    
    UIView *nameView = [[UIView alloc] init];
    nameView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1.0];
    [backbg_Add addSubview:nameView];
    nameView.frame = CGRectMake(0, 0, backbg_Add.frame.size.width, 40);
    UIButton *backBtn = [[UIButton alloc] init];
    [nameView addSubview:backBtn];
    backBtn.frame = CGRectMake(0, 0, 40, 40);
    [backBtn setImage:[[YZYSaccountStore sharedInstance] getImage:@"arrow_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(didClickBackBtn) forControlEvents:UIControlEventTouchUpInside];
    UILabel *son_titleLabel = [[UILabel alloc] init];
    son_titleLabel.font = [UIFont systemFontOfSize:16];
    son_titleLabel.text = @"添加小号";
    son_titleLabel.textColor = yzy_labelColor;
    [nameView addSubview:son_titleLabel];
    son_titleLabel.frame = CGRectMake(100, 0, 120, nameView.frame.size.height);
    self.son_titleLabel = son_titleLabel;
    
    UILabel *agreeLabel = [[UILabel alloc] init];
    [backbg_Add addSubview:agreeLabel];
    agreeLabel.frame = CGRectMake(20, CGRectGetMaxY(nameView.frame) + 40, backbg_Add.frame.size.width - 40, 22);
    agreeLabel.text = @"支持中英文和数字，不超过24个字符";
    agreeLabel.textColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0];
    agreeLabel.font = [UIFont systemFontOfSize:10];
    [agreeLabel sizeToFit];
    
    self.sonUserTextfield = [[UITextField alloc] init];
    self.sonUserTextfield.placeholder = @"请输入小号名称";
    self.sonUserTextfield.delegate = self;
    self.sonUserTextfield.font = [UIFont systemFontOfSize:16];
    self.sonUserTextfield.borderStyle = UITextBorderStyleRoundedRect;
    self.sonUserTextfield.returnKeyType = UIReturnKeyDone;
    [backbg_Add addSubview:self.sonUserTextfield];
    self.sonUserTextfield.frame = CGRectMake(20, CGRectGetMaxY(agreeLabel.frame) + 15, backbg_Add.frame.size.width - 40, 40);
    
    UIView *defaultView = [[UIView alloc] init];
//    [backbg_Add addSubview:defaultView];
    defaultView.frame = CGRectMake(20, CGRectGetMaxY(self.sonUserTextfield.frame) + 10, backbg_Add.frame.size.width - 50, 22);
    self.defaultBtn = [[UIButton alloc] init];
    [self.defaultBtn setImage:[[YZYSaccountStore sharedInstance] getImage:@"agreeBtn_unselected"] forState:UIControlStateNormal];
    [self.defaultBtn setImage:[[YZYSaccountStore sharedInstance] getImage:@"agreeBtn_selected"] forState:UIControlStateSelected];
    [self.defaultBtn addTarget:self action:@selector(didClickDefaultBtn) forControlEvents:UIControlEventTouchUpInside];
    [defaultView addSubview:self.defaultBtn];
    self.defaultBtn.frame = CGRectMake(0, 0, 22, 22);
    self.defaultBtn.selected = NO;
    UILabel *defaultLabel = [[UILabel alloc] init];
    [defaultView addSubview:defaultLabel];
    defaultLabel.frame = CGRectMake(CGRectGetMaxX(self.defaultBtn.frame), 0, 100, 22);
    defaultLabel.text = @"是否默认";
    defaultLabel.textColor = [UIColor colorWithRed:155/255.0 green:155/255.0 blue:155/255.0 alpha:1.0];
    defaultLabel.font = [UIFont systemFontOfSize:14];
    [defaultLabel sizeToFit];
    
    self.sonUserBtn = [[UIButton alloc] init];
    self.sonUserBtn.layer.cornerRadius = 5;
    self.sonUserBtn.layer.masksToBounds = YES;
    self.sonUserBtn.backgroundColor = yzy_themeColor;
    self.sonUserBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.sonUserBtn setTitle:@"添  加" forState:UIControlStateNormal];
    [self.sonUserBtn addTarget:self action:@selector(didClickConfirm) forControlEvents:UIControlEventTouchUpInside];
    [backbg_Add addSubview:self.sonUserBtn];
    self.sonUserBtn.frame = CGRectMake(20, CGRectGetMaxY(defaultView.frame) + 20, backbg_Add.frame.size.width - 40, 40);
}

#pragma UITableViewDataSource, UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sonUserArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YZYSonUserListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YZYSonUserListCell class]) forIndexPath:indexPath];
    cell.delegate = self;
    [cell dataWithDict:self.sonUserArray[indexPath.row] withIndex:indexPath.row withSelectIndex:self.currentSelectedIndex];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    self.currentSelectedIndex = indexPath.row;
    [tableView reloadData];
    NSNumber *sonId = [ZCFunDataFormat filterNumber:self.sonUserArray[indexPath.row][@"sonId"]];
    self.sonUserBlock(0, sonId);
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    });
}
- (void)sonUserNameDidSelect:(NSInteger)index {
    NSDictionary *dict = self.sonUserArray[index];
    self.sonId_Add = [ZCFunDataFormat filterNumber:dict[@"sonId"]];
    self.defaultBtn.selected = [[ZCFunDataFormat filterNumber:dict[@"isDefault"]] boolValue];
    self.sonUserTextfield.text = [ZCFunDataFormat filterString:dict[@"sonNickName"]];
    self.son_titleLabel.text = @"修改小号";
    [self.sonUserBtn setTitle:@"修  改" forState:UIControlStateNormal];
    [self.scrollview setContentOffset:CGPointMake(popupW, 0) animated:YES];
}

- (void)didClickChangeBtn {
    NSLog(@"切换账号");//相当于退出登录，回到手机号登录页面
    self.sonUserBlock(2, @"切换账号");
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    });
}

- (void)didClickDelegateBtn {
    NSLog(@"删除账号");//加入黑名单，回到手机号登录页面
    [[YZYSDKManager shareManager] yzy_logoutPlatformForAccountDelete];
}

- (void)didClickAddBtn {
    NSLog(@"添加小号");
    self.defaultBtn.selected = NO;//重置默认值
    self.sonId_Add = @0;//从添加入口进，需要清空编辑状态下的小号id
    self.sonUserTextfield.text = @"";
    self.son_titleLabel.text = @"添加小号";
    [self.sonUserBtn setTitle:@"添  加" forState:UIControlStateNormal];
    [self.scrollview setContentOffset:CGPointMake(popupW, 0) animated:YES];

}

- (void)didClickBackBtn {
    NSLog(@"返回小号列表");
    [self endEditing:YES];
    self.sonUserTextfield.text = @"";
    [self.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (void)didClickConfirm {
    NSLog(@"确认添加小号");
    [self endEditing:YES];
    NSString *sonUserText = self.sonUserTextfield.text;
    typeof(self) __weak weakSelf = self;
    if ([self.sonId_Add intValue] == 0) {
        //添加
        [self requestAddUserSonInfo:sonUserText completeHandle:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf requestGetUserSonInfoList];
                [weakSelf.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
            });
        }];
    } else {
        //编辑
        [self requestUpdateSonId:self.sonId_Add sonNickName:self.sonUserTextfield.text completeHandle:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf requestGetUserSonInfoList];
                [weakSelf.scrollview setContentOffset:CGPointMake(0, 0) animated:YES];
                self.sonId_Add = @0;//返回后清空编辑状态下的小号id
            });
        }];
    }
}
- (void)didClickDefaultBtn {
    NSLog(@"点击默认");
    self.defaultBtn.selected = !self.defaultBtn.selected;
}


///添加子用户
- (void)requestAddUserSonInfo:(NSString *)sonNickName completeHandle:(void(^)(void))handle {
    NSDictionary *params = @{
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"userId": self.userId,
        @"isDefault": @(self.defaultBtn.selected),
        @"sonNickName": sonNickName
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_addUserSonInfo) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        if ([surpercode isEqualToString:@"200"]) {
            handle();
        }
    } failure:^(NSString *error) {
        
    }];
}
///获取子用户列表
- (void)requestGetUserSonInfoList {
    NSDictionary *params = @{
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"userId": self.userId
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_getUserSonInfoList) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        if ([surpercode isEqualToString:@"200"]) {
            NSArray *resultArr = [NSArray arrayWithArray:surperdic[@"data"]];
            self.sonUserArray = [NSMutableArray arrayWithArray:resultArr];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
        
    } failure:^(NSString *error) {
        
    }];
}
///修改用户的小号默认值
- (void)requestUpdateDefaultWithSonId:(NSNumber *)sonId completeHandle:(void(^)(void))handle {
    NSDictionary *params = @{
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"userId": self.userId,
        @"isDefault": @(self.defaultBtn.selected),
        @"sonId": sonId
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_updateDefault) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        if ([surpercode isEqualToString:@"200"]) {
            handle();
        }
        
    } failure:^(NSString *error) {
        
    }];
}
///用户的小号绑定游戏id
- (void)requestUpdateGameIdWithSonId:(NSNumber *)sonId isDefault:(BOOL)isDefault {
    NSDictionary *params = @{
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"userId": self.userId,
        @"isDefault": @(isDefault),
        @"sonId": sonId
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_updateGameId) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        if ([surpercode isEqualToString:@"200"]) {
            self.sonUserBlock(0, sonId);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.coverView removeFromSuperview];
                [self removeFromSuperview];
            });
        } else {
            self.sonUserBlock(1, msg);
        }
        
    } failure:^(NSString *error) {
        self.sonUserBlock(1, error);
    }];
}
///修改用户的小号名称
- (void)requestUpdateSonId:(NSNumber *)sonId sonNickName:(NSString *)sonNickName completeHandle:(void(^)(void))handle {
    NSDictionary *params = @{
        @"gameId": [ZCFunDataFormat GetPlatformGameID],
        @"userId": self.userId,
        @"isDefault": @(self.defaultBtn.selected),
        @"sonId": sonId,
        @"sonNickName": sonNickName
    };
    [ZCNetworkManager postWithURL:YZY_AppendUrl(YZY_updateSonNickName) Params:params success:^(id responseObject) {
        NSDictionary *surperdic = (NSDictionary *)responseObject;
        NSString *surpercode = [NSString stringWithFormat:@"%@",surperdic[@"code"]];
//        NSString *msg = [NSString stringWithFormat:@"%@",surperdic[@"msg"]];
        if ([surpercode isEqualToString:@"200"]) {
            handle();
        }
        
    } failure:^(NSString *error) {
        
    }];
}

- (void)show {
    [[YZYSaccountStore sharedInstance] getCurrentWindowWithCompletion:^(UIWindow *window) {
        if (window) {
            UIButton *coverView = [[UIButton alloc] init];
            coverView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
            [coverView addTarget:self action:@selector(coverViewClick) forControlEvents:UIControlEventTouchUpInside];
            self.coverView = coverView;
            coverView.frame = window.bounds;
            
            UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
            UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            visualView.alpha = 0.0;
            [coverView addSubview:visualView];
            visualView.frame = coverView.frame;
            
            [window addSubview:coverView];
            
            self.frame = CGRectMake(0, 0, popupW, popupH);
            self.layer.cornerRadius = 12.0f;
            self.center = CGPointMake(window.center.x, window.center.y);
            if (![ZCFunDataFormat GetIsLandscape]) {
                self.center = CGPointMake(window.center.x, window.center.y - 40);
            }
            self.self_Height = self.frame.origin.y;
            [window addSubview:self];
        }
    }];
}

- (void)coverViewClick {
    [self endEditing:YES];
}

- (void)closeView {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    });
}

- (NSMutableArray *)sonUserArray {
    if (!_sonUserArray) {
        _sonUserArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _sonUserArray;
}

- (void)dealloc{
    //移除键盘通知监听者
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    if ([self.firstResponderTextF isFirstResponder])[self.firstResponderTextF resignFirstResponder];
}
#pragma maek UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    self.firstResponderTextF = textField;//当将要开始编辑的时候，获取当前的textField
    return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

#pragma mark : UIKeyboardWillShowNotification/UIKeyboardWillHideNotification
- (void)keyboardWillShow:(NSNotification *)notification{
    CGRect rect = [self.firstResponderTextF.superview convertRect:self.firstResponderTextF.frame toView:self];//获取相对于self.view的位置
    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];//获取弹出键盘的fame的value值
    CGRect keyboardRect = [aValue CGRectValue];
    keyboardRect = [self convertRect:keyboardRect fromView:self.window];//获取键盘相对于self.view的frame ，传window和传nil是一样的
    CGFloat keyboardTop = keyboardRect.origin.y;
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘弹出动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (keyboardTop < CGRectGetMaxY(rect)) {//如果键盘盖住了输入框
        CGFloat gap = keyboardTop - CGRectGetMaxY(rect) - 10;//计算需要网上移动的偏移量（输入框底部离键盘顶部为10的间距）
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.frame = CGRectMake(weakSelf.frame.origin.x, gap, weakSelf.frame.size.width, weakSelf.frame.size.height);
        }];
    }
    
}
- (void)keyboardWillHide:(NSNotification *)notification{
    NSDictionary *userInfo = [notification userInfo];
    NSNumber * animationDurationValue = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];//获取键盘隐藏动画时间值
    NSTimeInterval animationDuration = [animationDurationValue doubleValue];
    if (self.frame.origin.y < 0) {//如果有偏移，当影藏键盘的时候就复原
        __weak typeof(self)weakSelf = self;
        [UIView animateWithDuration:animationDuration animations:^{
            weakSelf.frame = CGRectMake(weakSelf.frame.origin.x, weakSelf.self_Height, weakSelf.frame.size.width, weakSelf.frame.size.height);
        }];
    }
}


@end
